<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-18 10:43
 *
 * 项目：rm  -  $  - VoteController.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\controllers;

use Lev;
use lev\base\Assetsv;
use lev\base\Controllerv;
use lev\base\Viewv;
use modules\levfm\helpers\UrlfmHelper;
use modules\levfm\table\form\FormData\FormData;

!defined('INLEV') && exit('Access Denied LEV');


class VoteController extends Controllerv
{

    public static $rendersDir = 'views/vote';

    public static $subnavs = [];

    public static function subnavs() {
        //static::setSubnav('same-ct', '历史同期', 2, 'home');
        return static::$subnavs;
    }

    public static function Form() {
        return FormDataController::Form();

    }

    /**
     * @param array $param
     *
     * @see UrlfmHelper::forms()
     */
    public static function actionIndex($param = [])
    {
        $param = $param ?: static::Form();
        if (!empty($param['setupDesc'])) {
            $keys = array_keys($param['setupDesc']);
            shuffle($keys);
            foreach ($keys as $k) {
                $name = $param['setupDesc'][$k];
                $url = Lev::toCurrent(['r'=>'vote/ajax-tab', 'setup'=>$k]);
                static::setSubnav($k, $name, 2, '', 'common', $url);
            }
        }

        $deTabId = Lev::stripTags(Lev::GPv('setup'));
        $subnavs = static::subnavs();

        $subnavInfo = isset($subnavs[$deTabId]) ? $subnavs[$deTabId] : reset($subnavs);

        $deTabId = $subnavInfo['tabid'];

        Lev::$app['title'] = $param['formInfo']['name'];

        Assetsv::highstockJs();
        Viewv::render(static::$rendersDir . '/index', $param + [
                'deTabId' => $deTabId,
                'subnavs' => $subnavs,
            ]);
    }

    public static function actionAjaxTab() {
        $tab = Lev::stripTags(Lev::GPv('tab'));

        if ($tab && method_exists(__CLASS__, $action = 'action'.Lev::ucfirstv($tab))) {
            static::$action();
        }else {
            Lev::showMessages(Lev::responseMsg(1, '', ['htms' => static::AjaxTabForm(), 'not' => 1]));
        }
    }

    private static function AjaxTabForm() {

        $param = static::Form();

        return Viewv::renderPartial(dirname(__DIR__) . '/template/renders/'.static::$rendersDir.'/tabs/form.php', $param);
    }

    public static function setSubnav($tabid, $name, $page = 2, $icon = '', $tmp = 'common', $url = null) {
        static::$subnavs[$tabid] = [
            'tabid' => $tabid,
            'name'  => $name,
            'icon'  => $icon ? '<svg class="icon"><use xlink:href="#fa-'.$icon.'"></use></svg>' : '',
            'url'   => $url !== null ? $url : Lev::toReRoute(['vote/ajax-tab', 'tab'=>$tabid]),
            'attr'  => '',//它应放在class=" {attr}" 例：attr = ' wdmin" target="_blank';
            'page'  => $page, //2,//-2：首次打开不加载url数据
            'not'   => 0,//1：没有数据了
            'tmp'   => $tmp,

            'loadStart'    => 'hiddenx',//hiddenx 隐藏
            'loadStartTip' => '点击加载更多',
            'width'        => '',
        ];
    }
}